'use strict';

stareal
    .controller("ViewSearchController", function ($scope, $api, $stateParams, $timeout, $lazyLoader,localStorageService,$alert) {
        // 展现热词
        $scope.hwf = true;
        var h=$(window).height()-1.4*parseFloat(document.documentElement.style.fontSize)+"px";
        $("#wrapper_container").height(h);
        //历史搜索
        $scope.history_list =new Array(8);
        if(localStorageService.get("viewsearchlist")!=undefined&&localStorageService.get("viewsearchlist")!=null&&localStorageService.get("viewsearchlist")!='') {
            $scope.history_list = JSON.parse(localStorageService.get("viewsearchlist"));
        }else{
            $scope.history_list=[];
        }
        if($scope.history_list){
            $scope.history = $scope.history_list;
        }else{
            $scope.history = [];
        }
        // console.log($scope.history)
        // $scope.history_list;
        //搜索方法
        var search = function(keyword){
            $timeout(function () {
                $scope.hwf = false;
                $scope.hwe = false;
                $scope.goods = new $lazyLoader("app/view/searchlist", {
                    name: keyword,
                    flag:1
                });
                $scope.goods.nextPage();
            }, 0);
        };
        //点击热词搜索
        $scope.hws = function(keyword){
            $scope.keyword = keyword;
            search(keyword);
        };
        //回车搜索
        var searchBtn = document.getElementById("searchBtn");
        var sou_iocnBtn1=document.getElementById('sou_iocnBtn1');
        var searchClear_icon1=document.getElementById('searchClear_icon1');
        searchBtn.onsearch = function(event){
            event.preventDefault();
            var keyword = this.value;

            if(keyword!=''&&keyword!=null&&keyword!=undefined) {
                search(keyword);
                $scope.history.unshift({"word": keyword});
                localStorageService.set("viewsearchlist", JSON.stringify($scope.history));
            }else{
                $alert.show('搜索内容不能为空!');
            }

        };
        sou_iocnBtn1.onclick=function(){
            var keyword = $('#searchBtn').val();
            if(keyword!=''&&keyword!=null&&keyword!=undefined) {
                search(keyword);
                $scope.history.unshift({"word": keyword});
                localStorageService.set("viewsearchlist", JSON.stringify($scope.history));
            }else{
                $timeout(function () {
                    $alert.show('搜索内容不能为空!');
                },0)
            }
        }
        searchClear_icon1.onclick=function(){
            $('#searchBtn').val('');
        }
        //输入没有内容
        searchBtn.oninput = function () {
            if(this.value.length<1){
                $scope.hwe = true;
                $scope.hwf = true;
                $scope.goods.nodata = false;
                $scope.history_list = JSON.parse(localStorageService.get("viewsearchlist"));
            }
        }
        $scope.dele = function (id) {
            $scope.history_list.splice(id,1 );
            $scope.history.splice(id,1 );
            localStorageService.set("viewsearchlist",JSON.stringify($scope.history_list));
        }
        $scope.clearAll=function(){
            $scope.history_list=[];
            $scope.history=[];
            localStorageService.set("viewsearchlist",'');
        }
        // localStorageService.remove("list")
    });